//
//  YCLSQLiteManager.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/28.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit
import FMDB
/*
 打开数据库
 创建数据表
 操作数据
 */



class YCLSQLiteManager: NSObject {

    //创建单例
    static let sharedSQLiteManager:YCLSQLiteManager = YCLSQLiteManager()
    
    let dbQueue: FMDatabaseQueue
    
    override init() {
       
        //数据库路径
        let dbPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/status.db"
        
        YCLPrint("dbPath: \(dbPath)")
        
        dbQueue = FMDatabaseQueue(path: dbPath)
        super.init()
        
        //创建表
        creatTable()
    }
    
    private func creatTable() {
    
        //读取文件中的sql语句来执行
        //获得文件路径
        let tablesPath = NSBundle.mainBundle().pathForResource("tables", ofType: "sql")
        
        //读取文件
        let tables = try! NSString.init(contentsOfFile: tablesPath!, encoding: NSUTF8StringEncoding)
        
//        YCLPrint("tables:\(tables)")
        
        //执行多条sql语句
        dbQueue.inDatabase { (db) in
            
            if db.executeStatements(tables as String){
            
//                YCLPrint("创建表成功")
            }else{
            
                YCLPrint("创建表失败")
            }
        }

    }

}
